/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.meraklisina.latexeditor;

import com.meraklisina.latexeditor.mdi.Common.Tools;
import com.meraklisina.latexeditor.mdi.DialogActions.ActionOpen;
import com.meraklisina.latexeditor.mdi.TextEditor.TextEditors;
import com.meraklisina.latexeditor.toolbarCustomize.CustomizeForm;
import com.meraklisina.latexeditor.mdi.DialogActions.ActionSave;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;

/**Thread class for autosave feature
 *
 * @author eozkanal
 */
public class AutoSaveThread extends java.lang.Thread{
    private BlueLatexEditor editor;
    private BlueLatexEditor getEditor()
    {
        return this.editor;
    }

    public AutoSaveThread(BlueLatexEditor editor)
    {
        this.editor=editor;
    }

    /** run method for thread calls.
     * Iterates over tabs and saves tab which are saved previously(have a file name) and has changes at every 5 minutes.
     */
    public void run()
    {
        ActionSave ClsActionSave = new ActionSave();

        while(true)
        {
            try
            {
                java.lang.Thread.sleep(30000);
            }
            catch(InterruptedException ex)
            {

            }

            TextEditors ClsTextEditor = new TextEditors();

            for(int i=0; i<ClsTextEditor.getEditors().size(); i++)
            {
                JTextPane tp=ClsTextEditor.GetJTextPaneAt(this.getEditor().tab_Editors, i);

                String filePath=ClsTextEditor.GetRealPath(i);

                if(!filePath.isEmpty() && ClsTextEditor.GetIsTextChanged(i))
                {
                    ClsActionSave.Save(this.getEditor().tab_Editors, i, filePath, false);
                }
            }
        }
    }
}